
#import "CustomTabBarController.h"
#import "HomePageViewController.h"
#import "TogetherViewController.h"
#import "CustomNavigationController.h"
#import "MemberViewController.h"
#import "AppDelegate.h"
#import "InfoViewController.h"

@implementation CustomTabBarController


- (void)viewDidLoad {
    [super viewDidLoad];
	[self hideExistingTabBar];
    NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:@"TabBarView" owner:self options:nil];
    self.tabBarView = [nibObjects objectAtIndex:0];
	self.tabBarView.homeButton.selected = YES;
    self.tabBarView.delegate = self;
    self.tabBarView.frame = self.tabBar.frame;
    [self.view addSubview:self.tabBarView];
    NSMutableArray* array = [NSMutableArray arrayWithObjects:
                            [self createViewControllerWithClass:[HomePageViewController class] andXibName:@"HomePageViewController"],
                             [self createViewControllerWithClass:[TogetherViewController class]  andXibName:nil],
                             [self createViewControllerWithClass:[MemberViewController class]  andXibName:nil],
                             [self createViewControllerWithClass:[InfoViewController class]  andXibName:@"info"],
							 nil];
	self.viewControllers = array;
}
- (UIViewController *)createViewControllerWithClass:(Class)classname andXibName:(NSString*)xibName{
    	
    if ([xibName isEqualToString:@"info"]) {
          UIViewController* viewController = [[classname alloc] initWithNibName:nil bundle:nil];
        return viewController;
    }else{
        UIViewController* viewController = [[classname alloc] initWithNibName:xibName bundle:nil];
        CustomNavigationController *navigationBarController = [[CustomNavigationController alloc] initWithRootViewController:viewController];
        return navigationBarController;
    }
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

// 在iOS6下已废弃
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return interfaceOrientation==UIInterfaceOrientationPortrait;
}


// Only on iOS6
- (BOOL)shouldAutorotate
{
    return NO;
}
// Only on iOS6
- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}




- (void)dealloc {

}

- (void)hideExistingTabBar {
	for(UIView *view in self.view.subviews) {
		if([view isKindOfClass:[UITabBar class]]) {
            view.superview.backgroundColor=[UIColor whiteColor];
			view.hidden = YES;
			break;
		}
	}
}

#pragma mark -
#pragma mark TabBarDelegate
-(void)tabWasSelected:(NSInteger)index {
    
    HomePageViewController *homePageViewController=(HomePageViewController*)[[[self.viewControllers objectAtIndex:0] viewControllers] objectAtIndex:0];
       [homePageViewController swipeLeft:nil];
       [homePageViewController hideRightLevelStatus];
        self.selectedIndex = index;
    if (index>2) {
        return;
    }
     AppDelegate *appDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
    if (appDelegate.menuQzdict==nil) {
        [homePageViewController performSelector:@selector(showdataForClicktag:andParam:) withObject:@"-1" withObject:nil];
    }else{
      [homePageViewController performSelector:@selector(showdataForClicktag:andParam:) withObject:@"0" withObject:appDelegate.menuQzdict];
    }
   
}


@end
